package com.zhike.app.dws;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.PropertyNamingStrategy;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.zhike.bean.AlarmBean;
import com.zhike.function.BeanToJsonFunction;
import com.zhike.utils.DateFormatUtil;
import com.zhike.utils.DorisUtils;
import com.zhike.utils.KafkaUtils;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.time.Duration;

/**
 * @Author: zhike
 * @CreateTime: 2024/2/26 9:33
 * @Description:  计算各车的告警信息并写入Doris
 */
public class DwsAlarmWindow {
    public static void main(String[] args) {
        //1.获取执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.enableCheckpointing(10000);
        env.setParallelism(2);

        //2.读取故障告警日志创建流并转换数据类型
        SingleOutputStreamOperator<JSONObject> alarmDS = env.fromSource(KafkaUtils.getKafkaSource("car_alarm_log", "dws_alarm_window"),
                        WatermarkStrategy.noWatermarks(), "alarm_log_source")
                .map(JSONObject::parseObject);

        //3.将流数据封装为AlarmBean对象
        SingleOutputStreamOperator<AlarmBean> alarmBeanDS = alarmDS.map(new MapFunction<JSONObject, AlarmBean>() {
            @Override
            public AlarmBean map(JSONObject value) throws Exception {
                //获取vin
                String vin = value.getString("vin");
                //获取告警级别
                Integer alarmLevel = value.getInteger("alarm_level");
                //获取时间戳
                Long ts = value.getLong("timestamp");

                return AlarmBean.builder()
                        .vin(vin)
                        .alarmCount(1)
                        .alarmCountL1(alarmLevel == 1 ? 1 : 0)
                        .alarmCountL2(alarmLevel == 2 ? 1 : 0)
                        .alarmCountL3(alarmLevel == 3 ? 1 : 0)
                        .ts(ts)
                        .build();
            }
        });

        //4.设置水位线
        SingleOutputStreamOperator<AlarmBean> alarmBeanWMDS = alarmBeanDS.assignTimestampsAndWatermarks(WatermarkStrategy
                .<AlarmBean>forBoundedOutOfOrderness(Duration.ofSeconds(30L))
                .withTimestampAssigner(new SerializableTimestampAssigner<AlarmBean>() {
                    @Override
                    public long extractTimestamp(AlarmBean element, long recordTimestamp) {
                        return element.getTs();
                    }
                }));

        //5.按照vin分组
        KeyedStream<AlarmBean, String> keyedDS= alarmBeanWMDS.keyBy(new KeySelector<AlarmBean, String>() {
            @Override
            public String getKey(AlarmBean value) throws Exception {
                return value.getVin();
            }
        });

        //6.开窗聚合
        SingleOutputStreamOperator<AlarmBean> reduceDS = keyedDS.window(TumblingEventTimeWindows.of(Time.seconds(60L)))
                .reduce(new ReduceFunction<AlarmBean>() {
                    @Override
                    public AlarmBean reduce(AlarmBean value1, AlarmBean value2) throws Exception {
                        value1.setAlarmCount(value1.getAlarmCount() + value2.getAlarmCount());
                        value1.setAlarmCountL1(value1.getAlarmCountL1() + value2.getAlarmCountL1());
                        value1.setAlarmCountL2(value1.getAlarmCountL2() + value2.getAlarmCountL2());
                        value1.setAlarmCountL3(value1.getAlarmCountL3() + value2.getAlarmCountL3());
                        return value1;
                    }
                }, new WindowFunction<AlarmBean, AlarmBean, String, TimeWindow>() {
                    @Override
                    public void apply(String s, TimeWindow window, Iterable<AlarmBean> input, Collector<AlarmBean> out) throws Exception {
                        AlarmBean bean = input.iterator().next();
                        bean.setStt(DateFormatUtil.toYmdHms(window.getStart()));
                        bean.setEdt(DateFormatUtil.toYmdHms(window.getEnd()));
                        bean.setWindowDate(DateFormatUtil.toDate(window.getStart()));
                        bean.setTs(System.currentTimeMillis());
                        out.collect(bean);
                    }
                });

        reduceDS.print();
        //7.将结果写入Doris表
        SingleOutputStreamOperator<String> resultDS = reduceDS.map(new MapFunction<AlarmBean, String>() {
            @Override
            public String map(AlarmBean value) throws Exception {
                return BeanToJsonFunction.getString(value);
            }
        });

        resultDS.print();
        resultDS.sinkTo(DorisUtils.getDorisSink("dws_alarm_window"));

        try {
            env.execute();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
